package com.qf.java2102.qihaonan.hotel.controller.order;

import com.qf.java2102.qihaonan.hotel.entity.CartItem;
import com.qf.java2102.qihaonan.hotel.entity.ResultVO;
import com.qf.java2102.qihaonan.hotel.pojo.User;
import com.qf.java2102.qihaonan.hotel.service.ICartService;
import com.qf.java2102.qihaonan.hotel.service.IOrderService;
import com.qf.java2102.qihaonan.hotel.service.impl.CartServiceImpl;
import com.qf.java2102.qihaonan.hotel.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author qhn
 * @version 1.0
 */
@WebServlet("/order/genernate")
public class OrderGenernateController  extends HttpServlet {

    private ICartService cartService = new CartServiceImpl();
    private IOrderService orderService = new OrderServiceImpl();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User frontUser = (User) session.getAttribute("frontUser");
        Map<Long, CartItem> cart = (Map<Long, CartItem>) session.getAttribute(frontUser.getUserId() + "");

        //订单
        ResultVO<Map> resultVO = orderService.generate(cart, frontUser);
        /**
         * //订单编号
         * map.put("orderId", order.getOrderId());
         * //订单总金额
         * map.put("orderTotalPrice", order.getOrderTotalPrice());
         * //订单明细列表
         * map.put("orderDetails", orderDetails);
         */
        //展示数据
        if(resultVO.getSuccess()) {
            //传数据
            request.setAttribute("orderId", resultVO.getData().get("orderId"));
            request.setAttribute("orderTotalPrice", resultVO.getData().get("orderTotalPrice"));
            request.setAttribute("orderDetails", resultVO.getData().get("orderDetails"));

            //清空餐车数据
            session.removeAttribute(frontUser.getUserId() + "");

            request.getRequestDispatcher("/front/detail/clientOrderList.jsp").forward(request, response);
        } else {
            response.getWriter().write(resultVO.getMessage());
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}